/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import DTO.NguoiDung;
import DTO.UserList;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Tu Nguyen
 */
@WebServlet(urlPatterns = {"/DangKy"})
public class DangKy extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            /* TODO output your page here. You may use following sample code. */
            response.sendRedirect("Login.jsp");
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        try (PrintWriter out = response.getWriter()) {
            String txtUser = request.getParameter("txtUser");
            String txtPass = request.getParameter("txtPass2");
            String txtHoTen = request.getParameter("txtHoTen");
            String txtCMND = request.getParameter("txtCMND");
            int txtGioiTinh = Integer.parseInt(request.getParameter("selGioiTinh"));
            String txtDienThoai = request.getParameter("txtDienThoai");
            String txtDiaChi = request.getParameter("txtDiaChi");
            String txtNgaySinh = request.getParameter("txtNgaySinh");
            int txtId = Integer.parseInt(request.getParameter("txtId"));
            NguoiDung nd = new NguoiDung();
            nd.Id = txtId;
            nd.CMND = txtCMND;
            nd.DiaChi = txtDiaChi;
            nd.DienThoai = txtDienThoai;
            if (txtGioiTinh == 0) {
                nd.GioiTinh = "Nam";
            } else {
                nd.GioiTinh = "Nu";
            }
            nd.HoTen = txtHoTen;
            nd.LoaiNguoiDung = 1;
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            nd.NgaySinh = txtNgaySinh;
            nd.NgayTao = new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime());
            // tao tài khoan nguoi dung
            BUS.NguoiDungBUS.Them(nd);
            UserList us = new UserList();
            us.Id = txtId;
            us.IdGroup = "KH";
            us.KichHoat = 1;
            us.NgayTao = new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime());
            us.Pass = txtPass;
            us.UserName = txtUser;
            BUS.UserListBUS.Them(us);
            //response.sendRedirect("Login.jsp");
//            if (request.getParameter("txtUser") != null) {
                processRequest(request, response);
            //}
        } catch (Exception ex) {
            ex.getStackTrace();
            //response.sendRedirect("DangKy.jsp");
        }

        
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
